package cc.ding.dao.Impl;

import cc.ding.dao.BaseDao;
import cc.ding.dao.StatisticsDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StatisticsDaoImpl extends BaseDao implements StatisticsDao {
    @Override
    public List<Map<String, String>> empStatistics(Integer empId) {
        List<Map<String,String>> list = new ArrayList();

        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT employeeId, date,DAYNAME(date) AS dayType,startTime,endTime,");
        sb.append(" CASE ");
        sb.append(" WHEN startTime = '' or endTime = '' THEN '上班或下班忘打卡'");
        sb.append(" WHEN startTime > '09:00:00' THEN '迟到'");
        sb.append(" WHEN endTime < '17:00:00' THEN '早退'");
        sb.append(" ELSE '正常打卡'");
        sb.append(" END AS attendance_status");
        sb.append(" FROM attendance");
        sb.append(" WHERE");
        sb.append(" employeeId = ?");
        sb.append(" ORDER BY date DESC");
        ResultSet resultSet = executeQuery(sb.toString(),empId);
        if (resultSet!=null){
            try {
                while (resultSet.next()){
                    Map<String,String> map = new HashMap<>();
                    map.put("employeeId", String.valueOf(resultSet.getInt("employeeId")));
                    map.put("date",resultSet.getString("date"));
                    map.put("dayType",resultSet.getString("dayType"));
                    map.put("startTime",resultSet.getString("startTime")==null?"":resultSet.getString("startTime"));
                    map.put("endTime",resultSet.getString("endTime")==null?"":resultSet.getString("endTime"));
                    map.put("attendance_status",resultSet.getString("attendance_status"));
                    list.add(map);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }

        return list;
    }
}
